//
//  ModalViewController.m
//  suRead
//
//  Created by 周中华 on 16/6/25.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "ModalViewController.h"
#import "AccountManager.h"
#import "UIApplication+VisibleViewController.h"

#import "ServiceUser.h"
#import "UIViewController+HUD.h"
#import "AccountManager.h"

#import "AppDelegate.h"
#import "Masonry.h"

@implementation ModalViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.view.backgroundColor = [UIColor lightGrayColor];
  
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
//    [button setTitle:@"Dismiss me" forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    _cpView = [ChangePhoneView changePhoneView];

    [_cpView.logoutButton addTarget:self action:@selector(logout:) forControlEvents:UIControlEventTouchUpInside];
    [_cpView.submitButton addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_cpView];
    [_cpView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_cpView.superview);
    }];
   // [self.view addSubview:button];
   
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

-(void) initStyle
{
    self.view.backgroundColor=[UIColor colorWithWhite:0 alpha:0.4];
    self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    
    _tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletapPressGesture:)];
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    [app.window addGestureRecognizer:_tapGesture];
}

-(void) dismiss
{
    [self dismissViewControllerAnimated:YES completion:^{
        AppDelegate *app = [[UIApplication sharedApplication] delegate];
        [app.window removeGestureRecognizer:_tapGesture];
        
    }];
    

}

BOOL CGPointInRect(CGPoint point, CGRect rect)
{
    if ((rect.origin.x <= point.x) &&
        (rect.origin.y <= point.y) &&
        (point.x <= (rect.origin.x + rect.size.width)) &&
        (point.y <= (rect.origin.y + rect.size.height)))
    {
        return YES;
    }
    return NO;
}

-(void)handletapPressGesture:(UITapGestureRecognizer*)sender{
    CGPoint point = [sender locationInView:self.cpView];
    CGRect subviewRect = [self.cpView.contentView convertRect:self.cpView.contentView.frame toView:self.view];
    
    if (!CGPointInRect(point, subviewRect)){
        [self dismissViewControllerAnimated:YES
                                 completion:^{
                                     //一定要移除手势 否则下次 没有子视图的时候 点击 会崩溃拉
                                     AppDelegate *app = [[UIApplication sharedApplication] delegate];
                                     [app.window removeGestureRecognizer:sender];
                                     sender.view.superview.backgroundColor = [UIColor clearColor];
                                 }];
    }
    
}

-(void)submit:(id)sender{
    if(_cpView.pwdText.text.length==0) {
        [[[[UIApplication sharedApplication] keyWindow] rootViewController] showHUDText:@"请输入密码"];
        return;
    }
    
    [ServiceUser doThirdPartBindingPhone:[AccountManager sharedAccountManager].f_uid
                                phoneNum:_cpView.phoneText.text
                                    code:_cpView.codeText.text
                                password:_cpView.pwdText.text
                                 success:^(id responseObject, NSString *message) {
                                     [[[[UIApplication sharedApplication] keyWindow] rootViewController] showHUDText:message];
                                     
                                                                       //                                     self.phoneText.text = @"";
                                     //                                     self.codeText.text = @"";
                                     //                                     self.pwdText.text=@"";
                                     [AccountManager sharedAccountManager].f_phone_num =_cpView.phoneText.text;
                                       [self dismiss];
                                     //                                     weak_self.getCodeButton.userInteractionEnabled=YES;
                                     //                                     weak_self.getCodeButton.alpha=1.0f;
                                 } failure:^(NSString *message) {
                                     [[[[UIApplication sharedApplication] keyWindow] rootViewController] showHUDText:message];
                                     
                                   
                                 }];

}

-(void) logout:(id)sender
{
    
    [AccountManager sharedAccountManager].f_isLogined = NO;
    [AccountManager sharedAccountManager].f_nickname = @"";
    [AccountManager sharedAccountManager].f_email = @"";
    [AccountManager sharedAccountManager].f_uid = @"";
    [AccountManager sharedAccountManager].f_userName = @"";
    [self dismissViewControllerAnimated:YES completion:^{
        AppDelegate *app = [[UIApplication sharedApplication] delegate];
        [app.window removeGestureRecognizer:_tapGesture];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"do" object:self];
    }];

    if ([self.delegate isKindOfClass:[UIViewController class]]) {
        [((UIViewController *)self.delegate).navigationController popToRootViewControllerAnimated:YES];
    }
    
    

    
}
-(void) buttonClicked:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(modalViewControllerDidClickedDismissButton:)]) {
        [self.delegate modalViewControllerDidClickedDismissButton:self];
    }
}

@end
